﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using BrainTechLLC;
using ImageAppDataModels;
using ImageAppViewModels;

namespace SettingsWindowsHandler.Plugin
{
    /// <summary>
    /// Interaction logic for SettingsView.xaml
    /// </summary>
    public partial class SettingsView : UserControl
    {
        public event EventHandler RequestClose;

        public static readonly DependencyProperty SettingsViewModelProperty =
            DependencyProperty.Register("SettingsViewModel", typeof(SettingsViewModel), typeof(SettingsView), new PropertyMetadata(null, OnSettingsViewModelPropertyChanged));

        public SettingsViewModel SettingsViewModel
        {
            get { return (SettingsViewModel)GetValue(SettingsViewModelProperty); }
            set { SetValue(SettingsViewModelProperty, value); }
        }

        private static void OnSettingsViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SettingsView source = d as SettingsView;
            SettingsViewModel vNew = e.NewValue as SettingsViewModel;
            SettingsViewModel vOld = e.OldValue as SettingsViewModel;
            source.UpdateSettingsViewModel(vNew, vOld);
        }

        private void UpdateSettingsViewModel(SettingsViewModel viewModel, SettingsViewModel viewModelOld)
        {
            if (viewModel == viewModelOld)
                return;

            if (viewModelOld != null)
            {
                viewModelOld.PropertyChanged -= viewModel_PropertyChanged;
            }

            if (viewModel != null)
            {
                //Let the user get to their tools & making reuests even ewhen the bank syste is donw.   rework for then firty afternoons iterator sfeeels like for Measure...
                viewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(viewModel_PropertyChanged);
            }

            SettingsViewModel = viewModel;

            // Let the UI bind to the view-model.
            this.DataContext = SettingsViewModel;
        }

        void viewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            // TODO: HEREHERE
            //if (e.PropertyName == "BlurCutoff" || e.PropertyName == "BlurCutoff")
            //{
            //    Window1.MainWindow.ImageCollectionViewModel.UpdateBlur();
            //}            
        }
        
        public SettingsView()
        {
            InitializeComponent();            
        }

        private void btnSaveExit_Click(object sender, RoutedEventArgs e)
        {
            SettingsWindowHandler.Handler.Handler.SettingsWatcher.SaveData();
            RequestClose.RaiseEvent(this);
        }        

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            SettingsWindowHandler.Handler.Handler.SettingsWatcher.SaveData();
        }
    }
}
